#include "forms.h"
#include "ui_forms.h"
#include"studentinfo.h"
#include "execsql.h"
#include <QMessageBox>
#include <QString>
#include<QSqlQuery>
#include <QSqlRecord>
#include<QDebug>
#include"student.h"

FormS::FormS(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::FormS)
{
    ui->setupUi(this);
}

FormS::~FormS()
{
    delete ui;
}

void FormS::showformS()
  {
    emit signalformS();
    this->show();
  }

void FormS::on_pb_ok_clicked()
{
   id =ui->lineEditS_id->text();
   ExecSQL exe;
   sinfo = exe.selectStudentInfo(id);
    if(id == sinfo.getID() )
    {
        if(ui->lineEditS_name->text() == sinfo.getname())
        {
            if(ui->lineEditS_pswd->text()== ui->lineEditS_again->text())
            {
                QSqlQuery query;
                QString queryString =
                        QString("update StudentInfoTable set loginpw = "+ui->lineEditS_again->text() +
                                " where ID  = '%1'").arg(id);
                if(query.exec(queryString))
                {
                    int ID_id = query.record().indexOf("ID");
                    int xuehao_id = query.record().indexOf("xuehao");
                    int Name_id = query.record().indexOf("Name");
                    int Sex_id = query.record().indexOf("Sex");
                    int Classid_id = query.record().indexOf("Classid");
                    int loginpw_id = query.record().indexOf("loginpw");
                    int RxTime_id = query.record().indexOf("RxTime");
                    while(query.next())
                    {
                        QString id = query.value(ID_id).toString();
                        QString Name = query.value(Name_id).toString();
                        QString XuehaoID = query.value(xuehao_id).toString();
                        QString pswd = query.value(loginpw_id).toString();
                        QString Classid = query.value(Classid_id).toString();
                        QString Sex = query.value(Sex_id).toString();
                        QString rxtime = query.value(RxTime_id).toString();
                        sinfo.updateStudentInfo(id, Name,XuehaoID, pswd,Sex, Classid, pswd);
                        sinfo.display();
                      }
            }else{
                      qDebug()<<"---------999---------";
            }
            }
        }
    }else{
        QMessageBox msgBox;
        msgBox.setText("Error: Your ID is nonexistent !!");
        msgBox.setInformativeText("Please try again !");
        msgBox.setStandardButtons(QMessageBox::Retry | QMessageBox::Cancel);
        msgBox.setDefaultButton(QMessageBox::Retry);

        int ret = msgBox.exec();
        switch (ret) {
          case QMessageBox::Retry:
              ui->lineEditS_id->setFocus();
              break;
          case QMessageBox::Cancel:
              this->close();
              break;
          default:
              // should never be reached
              break;
        }
    }

}
void FormS::showSW()
{
    Student *sw = new Student;
    sw->show();
    sw->selectBoxSlot(str);
}
void FormS::on_pb_cancel_clicked()
{
    this->close();
   this->showSW();
}
